package com.wardrobe.soa.api.goods;

import com.wardrobe.dao.goods.GoodsDao;
import com.wardrobe.dao.goods_model.GoodsModelDao;
import com.wardrobe.dao.shop.ShopDao;
import com.wardrobe.dao.store.StoreDao;
import com.wardrobe.domain.goods.GoodsShowBean;
import com.wardrobe.domain.goods_model.GoodsModelBean;
import com.wardrobe.domain.shop.ShopShowBean;
import com.wardrobe.domain.store.StoreShowBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2018/11/20.
 */
@Service
public class GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private GoodsModelDao goodsModelDao;
    @Autowired
    private StoreDao storeDao;

    public GoodsResponse getGoodsDetail(long id,long userId){
        GoodsShowBean bean = goodsDao.selectGoodsById(id);
        ShopShowBean shop = shopDao.selectShopById(bean.getShopId());
        List<GoodsModelBean> goodsModel = goodsModelDao.selectGoodsModelByGoodsId(id);
        List<GoodsShowBean> goodsList = goodsDao.selectGoodsListByShopId(bean.getShopId());
        StoreShowBean storeShowBean = storeDao.selectStoreGoods(userId,id);
        boolean flag = false;
        long storeId = 0;
        if (storeShowBean!=null){
            flag = true;
            storeId = storeShowBean.getId();
        }
        return GoodsResponse.builder()
                .id(id)
                .showPicture(bean.getShowPicture().split(";"))
                .price(bean.getPrice())
                .name(bean.getGoodName())
                .goodsModelList(goodsModel)
                .flag(flag)
                .storeId(storeId)
                .shopId(bean.getShopId())
                .shopLogo(shop.getLogo())
                .shopName(shop.getShopName())
                .telphone(shop.getTelphone())
                .address(shop.getAddress())
                .detailPicture(bean.getDetailPicture())
                .goodsList(goodsList)
                .build();
    }

    public List<GoodsShowBean> getSearchGoodsList(String goodName){
        List<GoodsShowBean> list = goodsDao.getSearchGoodsList(goodName);
        list.stream().forEach(x->{
            x.setCreateTime(x.getCreateTime().substring(0,x.getCreateTime().length()-2));
            x.setUpdateTime(x.getUpdateTime().substring(0,x.getUpdateTime().length()-2));
            x.setShowPicture(x.getShowPicture().split(";")[0]);
        });
        return list;
    }

    public List<GoodsShowBean> getHotGoodsList(){
        List<GoodsShowBean> list = goodsDao.selectApiGoodsList();
        list.stream().forEach(x->{
            x.setCreateTime(x.getCreateTime().substring(0,x.getCreateTime().length()-2));
            x.setUpdateTime(x.getUpdateTime().substring(0,x.getUpdateTime().length()-2));
            x.setShowPicture(x.getShowPicture().split(";")[0]);
        });
        List<GoodsShowBean> list2 = list.size()>10?list.subList(0,10):list;
        return list2;
    }
}
